package com.ruoyi.web.controller.shopping;

import com.ruoyi.common.utils.shopping.JwtUtil;
import com.ruoyi.system.domain.shopping.Product;
import com.ruoyi.system.domain.shopping.ShoppingCart;
import com.ruoyi.system.service.shopping.ShoppingCartService;
import com.ruoyi.system.view.common.Result;
import com.ruoyi.web.controller.shopping.common.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/xiaomi/v2/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectList(BaseContext.getId());
        return Result.success(shoppingCarts,"查询成功");
    }
    @PostMapping
    public Result add(@RequestBody Product product) {
        Boolean add = shoppingCartService.add(BaseContext.getId(), product.getProductId());
        return Result.success(null,add ? "操作成功" : "操作失败");
    }
    @GetMapping("/product/count")
    public Result count(){
        int count = shoppingCartService.count(BaseContext.getId());
        return Result.success(count,"查询成功");
    }
    @PutMapping("/product/num")
    public Result updateNum(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setUserId(BaseContext.getId());
        Boolean aBoolean = shoppingCartService.updateNum(shoppingCart);
        return Result.success(null,aBoolean ? "修改成功" : "修改失败");
    }
    @DeleteMapping("/{shoppingCartId}")
    public Result delete(@PathVariable Integer shoppingCartId){
        Boolean aBoolean = shoppingCartService.delete(shoppingCartId);
        return Result.success(null,aBoolean ? "删除成功" : "删除失败");
    }
}
